import { routerRedux } from 'dva/router';
import * as query from '../services/api';
import { setAuthority } from '@/utils/authority';
import { getPageQuery } from '@/utils/utils';
import { reloadAuthorized } from '@/utils/Authorized';

const namespace = 'login';
export default {
  namespace,

  state: {
    status: undefined,
    geeTest: null,
  },

  effects: {
    * login({ payload = {}, callback }, { call, put }) {
      const response = yield call(query.create, payload);

      if (callback) callback(response);
    },

    /**
     *  获取滑块儿验证码
     */
    * getGeeTest({ payload = {}, callback }, { call, put }) {
      const { errcode, data } = yield call(query.fetch, payload);

      if (errcode) {
        if (callback) callback({ errcode });
      } else {
        yield put({
          type: 'save',
          payload: {
            geeTest: data,
          },
        });
      }
    },
  },

  reducers: {
    changeLoginStatus(state, { payload = {} }) {
      setAuthority(payload.currentAuthority);
      return {
        ...state,
        errcode: payload.errcode,
        status: payload.status,
        type: payload.type,
      };
    },
    save(state, { payload = {} }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};